#include "settingdialog.h"

#include <QVBoxLayout>
#include <QFrame>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QHBoxLayout>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#include <QFile>
#include <QDebug>
#include <QColorDialog>

QJsonDocument *SettingDialog::document = nullptr;
int r_themeColor = 100,g_themeColor = 100,b_themeColor = 100;
int r_taskColor = 100,g_taskColor = 100,b_taskColor = 100;
SettingDialog::SettingDialog(QWidget *parent) : QDialog(parent),
    mainLayout(new QVBoxLayout),prefixLayout(new QHBoxLayout),groupBoxLayout(new QVBoxLayout),themeCorlorLayout(new QHBoxLayout),taskColorLayout(new QHBoxLayout),
    labelThemeColor(new QLabel),labelTaskColor(new QLabel),themeColor(new QPushButton),defaultTaskColor(new QPushButton),
    groupBox(new QGroupBox),sysTray(new QRadioButton),popUP(new QRadioButton),
    prefix(new QCheckBox),suffix(new QCheckBox),prefixEdit(new QLineEdit),
    splitter1(new QFrame),splitter2(new QFrame)
{
    Settings settings = getSettings();
    qDebug() << settings.themeColor << settings.taskColor
             << settings.isPopUP << settings.prefix << settings.enableSuffix;

    labelTaskColor->setText("任务默认颜色");
    labelThemeColor->setText("日历主题颜色");
    themeColor->setStyleSheet("background:rgb("+QString::number(r_themeColor)+","+QString::number(g_themeColor)+","+QString::number(b_themeColor)+")");
    defaultTaskColor->setStyleSheet("background:rgb("+QString::number(r_taskColor)+","+QString::number(g_taskColor)+","+QString::number(b_taskColor)+")");
    themeCorlorLayout->addWidget(labelThemeColor);
    themeCorlorLayout->addWidget(themeColor);
    taskColorLayout->addWidget(labelTaskColor);
    taskColorLayout->addWidget(defaultTaskColor);

    connect(themeColor,&QPushButton::clicked,this,&SettingDialog::SetThemeColor);
    connect(defaultTaskColor,&QPushButton::clicked,this,&SettingDialog::SetTaskColor);

    groupBox->setTitle("任务提醒方式");
    sysTray->setText("系统通知栏提醒");
    popUP->setText("弹出窗口提示");
    groupBoxLayout->addWidget(sysTray);
    groupBoxLayout->addWidget(popUP);
    sysTray->setChecked(true);
    groupBox->setLayout(groupBoxLayout);
    prefix->setText("为文件类任务自动添加前缀");
    prefixEdit->setText("处理文件");
    prefixEdit->setEnabled(false);
    suffix->setText("添加文件类任务时默认任务名包含文件名后缀");
    prefixLayout->addWidget(prefix);
    prefixLayout->addWidget(prefixEdit);
    splitter1->resize(splitter1->width(),1);
    splitter1->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
    splitter2->resize(splitter1->width(),1);
    splitter2->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);

    mainLayout->addLayout(themeCorlorLayout);
    mainLayout->addLayout(taskColorLayout);
    //mainLayout->addWidget(splitter1);
    //mainLayout->addWidget(groupBox);
    //mainLayout->addWidget(splitter2);
    //mainLayout->addLayout(prefixLayout);
    //mainLayout->addWidget(suffix);

    setLayout(mainLayout);

}


void SettingDialog::SetThemeColor()
{
    QColor c = QColorDialog::getColor(QColor::fromRgb(r_themeColor,g_themeColor,b_themeColor),this,"主题颜色选择");
    c.getRgb(&r_themeColor,&g_themeColor,&b_themeColor);
    //更新按钮颜色
    themeColor->setStyleSheet("background:rgb("+QString::number(r_themeColor)+","+QString::number(g_themeColor)+","+QString::number(b_themeColor)+")");
    WriteToJson(r_themeColor,g_themeColor,b_themeColor,r_taskColor,g_taskColor,b_taskColor);

    // 发射信号，将颜色值传递给Calendar类
    emit themeColorChanged(r_themeColor, g_themeColor, b_themeColor);
}

void SettingDialog::SetTaskColor()
{
    QColor c = QColorDialog::getColor(QColor::fromRgb(r_taskColor,g_taskColor,b_taskColor),this,"默认任务颜色选择");
    c.getRgb(&r_taskColor,&g_taskColor,&b_taskColor);
    //更新按钮颜色
    defaultTaskColor->setStyleSheet("background:rgb("+QString::number(r_taskColor)+","+QString::number(g_taskColor)+","+QString::number(b_taskColor)+")");
    WriteToJson(r_themeColor,g_themeColor,b_themeColor,r_taskColor,g_taskColor,b_taskColor);
}

void SettingDialog::WriteToJson(int r_themeColor,int g_themeColor,int b_themeColor,int r_taskColor,int g_taskColor,int b_taskColor)
{
    // 打开JSON文件
    QFile file("./settings.json");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        // 文件打开失败处理
        return;
    }

    // 读取JSON文件内容
    QByteArray jsonData = file.readAll();
    file.close();

    // 解析JSON数据
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
    if (jsonDoc.isNull())
    {
        // JSON解析失败处理
        return;
    }

    // 检索顶层JSON对象
    QJsonObject jsonObj = jsonDoc.object();

    // 更新themeColor的RGB值
    QJsonObject themeColorObj = jsonObj["themeColor"].toObject();
    themeColorObj["r"] = r_themeColor;
    themeColorObj["g"] = g_themeColor;
    themeColorObj["b"] = b_themeColor;
    jsonObj["themeColor"] = themeColorObj;

    qDebug() << "themeObject content: " << themeColorObj;

    // 更新taskColor的RGB值
    QJsonObject taskColorObj = jsonObj["taskColor"].toObject();
    taskColorObj["r"] = r_taskColor;
    taskColorObj["g"] = g_taskColor;
    taskColorObj["b"] = b_taskColor;
    jsonObj["taskColor"] = taskColorObj;

    qDebug() << "taskObject content: " << taskColorObj;

    // 创建新的JSON文档
    QJsonDocument newJsonDoc(jsonObj);

    // 打开JSON文件进行写入
    QFile file2("./settings.json");
    if (!file2.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        // 文件打开失败处理
        qDebug() << "fail to open file ";
        return;
    }

    // 将新的JSON文档写入文件
    file2.write(newJsonDoc.toJson());
    file2.close();
}

Settings SettingDialog::getSettings()
{
    //获取并显示文件中的颜色
    if(!QFile::exists("./settings.json")){
        qDebug() << "Could not find the file";
        setDefault();
    }
    QFile settingFile("./settings.json");
    if(!settingFile.open(QIODevice::ReadOnly)){
        qDebug() << "Could not open the file";
        return Settings();
    }
    QByteArray data = settingFile.readAll();
    qDebug() << "Data length: " << data.length();
    qDebug() << "Data content: " << data;
//    if(!document) document = new QJsonDocument;
//    document->fromJson(data);
    QJsonParseError error;
    QJsonDocument document = QJsonDocument::fromJson(data, &error);
    if (error.error != QJsonParseError::NoError) {
        qDebug() << "Failed to parse JSON data: " << error.errorString();
        return Settings();
    }

    QJsonObject object = document.object();
    //QJsonObject object = document->object();
    qDebug() << "Object content: " << object;
    Settings settings;
    QJsonObject tempObject;
    if(object.contains("themeColor")) tempObject = object["themeColor"].toObject();
    //int r,g,b;
    r_themeColor = tempObject["r"].toInt();
    g_themeColor = tempObject["g"].toInt();
    b_themeColor = tempObject["b"].toInt();
    settings.themeColor = QColor(r_themeColor,g_themeColor,b_themeColor);
    if(object.contains("taskColor")) tempObject = object["taskColor"].toObject();
    r_taskColor = tempObject["r"].toInt();
    g_taskColor = tempObject["g"].toInt();
    b_taskColor = tempObject["b"].toInt();
    settings.taskColor = QColor(r_taskColor,g_taskColor,b_taskColor);
    if(object.contains("isPopUP")) settings.isPopUP = object["isPopUp"].toBool();
    if(object.contains("prefix")) settings.prefix = object["prefix"].toString();
    if(object.contains("enableSuffix")) settings.enableSuffix = object["enableSuffix"].toBool();

    return settings;
}

void SettingDialog::setDefault()
{
    QFile settingFile("./settings.json");
    if(!settingFile.open(QIODevice::WriteOnly)){
        qDebug() << "Could not open the file!";
        return;
    }
    QJsonObject object;
    QJsonObject themeColorObject;
    QJsonObject taskColorObject;
    themeColorObject["r"] = 206;
    themeColorObject["g"] = 230;
    themeColorObject["b"] = 186;
    object["themeColor"] = themeColorObject;
    taskColorObject = themeColorObject;
    object["taskColor"] = taskColorObject;
    object["isPopUP"] = false;
    object["prefix"] = QString();
    object["enableSuffix"] = false;
    QJsonDocument document(object);
    settingFile.write(document.toJson());
}
